package com.example.campusforumapi.service.impl;

import com.example.campusforumapi.dao.UserMapper;
import com.example.campusforumapi.entity.Department;
import com.example.campusforumapi.entity.User;
import com.example.campusforumapi.service.UserService;
import com.example.campusforumapi.util.MD5Util;
import com.example.campusforumapi.util.PageQueryUtil;
import com.example.campusforumapi.util.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
   @Autowired
   private UserMapper userMapper;

    @Override
    public List<User> queryAll(){
        return userMapper.findAllUsers();
    }
    @Override
    public PageResult getUserPage(Integer page,Integer PageSize,Integer userId,String username,Integer departmentId,String gender){
////        当前页码中的数据列表
//        List<User> users = userMapper.findUsers(pageUtil);
////        数据总条数，用于计算分页数据
//        int total = userMapper.getTotalUser(pageUtil);
////        分页信息封装
//        PageResult pageResult = new PageResult(users,total,pageUtil.getLimit(),pageUtil.getPage());
//        return pageResult;
        //1.设置分页参数
        PageHelper.startPage(page,PageSize);
//        2.执行查询
        List<User> userList = userMapper.list(userId,username,departmentId,gender);
        Page<User> p = (Page<User>) userList;
//        3.封装PageResult对象
        PageResult pageResult = new PageResult(p.getResult(),p.getTotal(),p.getPageSize(),p.getPageNum());
        return pageResult;
    }
    @Override
    public int findsameUsername(String user){
        return userMapper.findsameUsername(user);
    }
    @Override
    public int findsameUserId(Integer userId){
        return userMapper.findsameUserId(userId);
    }
    @Override
    public int insertUser (User user){
        if(user.getPassword()!=null&&user.getPassword()!=""){
            user.setPassword(MD5Util.encrypByMd5(user.getPassword() + MD5Util.KEY));
        }else {
            user.setPassword(MD5Util.encrypByMd5("123456"+ MD5Util.KEY)) ;
        }
        user.setRegistertime(LocalDateTime.now());
        return userMapper.insertUser(user);
    }

    @Override
    public int updUser(User user){
        if(user.getPassword()!=null&&user.getPassword()!=""){
            user.setPassword(MD5Util.encrypByMd5(user.getPassword() + MD5Util.KEY));
        }
        user.setUpdatetime(LocalDateTime.now());//更新修改时间
        return userMapper.updUser(user);
    }

    @Override
    public int delUser(List<Integer> ids){ return userMapper.delUser(ids); }

    @Override
    public User getById(Integer id){
        return userMapper.getById(id);
    }

    @Override
    public List<Department> getDepartments() {
        return userMapper.getDepartments();
    }

    @Override
    public User login(User user) {
        user.setPassword(MD5Util.encrypByMd5(user.getPassword() + MD5Util.KEY));
        User u = userMapper.getByUsernameAndPassword(user);
        return u;
    }

    @Override
    public int findRoleId(User user) {
        return userMapper.findRoleId(user);
    }

    @Override
    public int getPassword(User user) {
        user.setPassword(MD5Util.encrypByMd5(user.getPassword() + MD5Util.KEY));
        return userMapper.getPassword(user);
    }
}
